﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BallWar
{
    /// <summary>
    /// 封装一个玩家
    /// </summary>
    class Game
    {
        /// <summary>
        /// 玩家的球
        /// </summary>
        public Ball ball { get; set; }
        public List<Bean> beans { get; set; }
        public int Mark { get; set; }
        public int Speed { get; set; }
        public Game(System.Drawing.Graphics g,Sence Sence)
        {
            ball = new Ball(System.Drawing.Color.Blue, new System.Drawing.Point(50, 50), g);
            ball.Display();
            Mark = 0;
            Speed = 10;
            beans = Sence.Beans;
        }
        int eaten = 0;
        public void Move()//移动方法
        {
            
            List<int> _deleteindex = new List<int>();//要删除的彩豆的索引集合
            foreach(Bean bean in beans)//遍历彩豆集合
            {
                if(Math.Sqrt(Math.Pow(ball._station.X-bean._station.X+ball._size.Width/2,2)+ Math.Pow(ball._station.Y - bean._station.Y+ball._size.Height/2, 2)) <= ball._size.Height/2)//若彩豆和球的距离小于求的半径
                {
                    bean.UnDisplay();//从屏幕上删除彩豆
                    ball.Grow();//球变大
                    _deleteindex.Add( beans.IndexOf(bean));//向集合中加入这个彩豆的索引
                }
            }
            
            foreach (int index in _deleteindex)//遍历要删除的彩豆索引集合
            {
                if (index < beans.Count)
                {
                    beans.RemoveAt(index);//删除对应的彩豆
                    Mark += 5;
                    eaten += 1;
                    if (eaten >= 10)
                    {
                        eaten = 0;
                        Speed =(int)(Speed*1.5);
                    }
                }
            }
            ball.Move();//移动球
        }
        public void Turn(Direction direction)
        {
            ball._direction = direction;
        }
    }

    
}
